package com.data.modules.bpm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.data.modules.bpm.service.IProcessService;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class ProcessServiceImpl implements IProcessService {

    @Resource
    private ProcessEngine processEngine;
    final String leaveProcessName="请假流程1.0";
    final String leaveProcessPath="flows/leave.bpmn";
    final String processDefinitionId = "act_leave";
    /**
     * 部署请假流程
     */
    @Override
    public void deployProcess(String proName,String proPath) {
        if(StrUtil.isEmpty(proName)){
            proPath = leaveProcessPath;
            proName = leaveProcessName;
        }
        log.info("{}【流程】部署开始",proName);
        Deployment deployment = processEngine.getRepositoryService().createDeployment().name(proName).addClasspathResource(proPath).deploy();

        log.info("{}【流程】部署完成,deployId:{}",proName, deployment.getId());
    }
    
    @Override
    public void destroyProces(String proDefId) {
        if(StrUtil.isEmpty(proDefId)){
            proDefId  =  processDefinitionId;
        }
        
        ProcessDefinition proDef = processEngine.getRepositoryService()
                .getProcessDefinition(proDefId);
        String deploymentId = proDef.getDeploymentId();
        log.info("deploymentId:{}",deploymentId);
        processEngine.getRepositoryService().deleteDeployment(deploymentId);
        
    }
    
    
}
